﻿using IPCLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
namespace IPCLibraryTest
{
    
    
    /// <summary>
    ///This is a test class for MemoryMappedFileTest and is intended
    ///to contain all MemoryMappedFileTest Unit Tests
    ///</summary>
	[TestClass()]
	public class MemoryMappedFileTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for Size
		///</summary>
		[TestMethod()]
		public void SizeTest()
		{
			int size = 1234;
			MemoryMappedFile target = new MemoryMappedFile(size);
			int actual;
			actual = target.Size;
			Assert.AreEqual(size, actual);
			target.Dispose();
		}

		/// <summary>
		///A test for Name
		///</summary>
		[TestMethod()]
		public void NameTest()
		{
			int size = 12;
			string name = "test";
			MemoryMappedFile target = new MemoryMappedFile(size, name);
			string actual;
			actual = target.Name;
			Assert.AreEqual(name, actual);
			target.Dispose();
		}

		/// <summary>
		///A test for WriteBytes
		///</summary>
		[TestMethod()]
		public void WriteBytesTest()
		{
			int size = 100;
			string name = "test";
			MemoryMappedFile target = new MemoryMappedFile(size, name);

			byte[] data = new byte[] { 1, 2, 3, 4, 5 };
			int offset = 0;
			int count = data.Length;
			target.WriteBytes(data, offset, count);

			byte[] result = target.ReadBytes(0, data.Length);

			Assert.AreEqual(data.Length, result.Length);
			for (int i = 0; i < data.Length; i++)
			{
				Assert.AreEqual(data[i], result[i]);
			}
			target.Dispose();
		}
		/// <summary>
		///A test for SynchronizedWrite
		///</summary>
		[TestMethod()]
		public void SynchronizedWriteTest()
		{
			int size = 100;
			string name = "test";
			MemoryMappedFile target = new MemoryMappedFile(size, name);

			byte[] data = new byte[] { 1, 2, 3, 4, 5 };
			int offset = 0;
			int count = data.Length;
			target.SynchronizedWrite(data, offset, count);

			byte[] result = target.ReadBytes(0, data.Length);

			Assert.AreEqual(data.Length, result.Length);
			for (int i = 0; i < data.Length; i++)
			{
				Assert.AreEqual(data[i], result[i]);
			}
			target.Dispose();
		}

		/// <summary>
		///A test for ReadBytes
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void ReadBytesTest()
		{
			int size = 100;
			string name = "test";
			MemoryMappedFile target = new MemoryMappedFile(size, name);

			byte[] result = target.ReadBytes(0, size);
			Assert.AreEqual(size, result.Length);

			byte[] data = new byte[] { 1, 2, 3, 4, 5 };
			int offset = 0;
			int count = data.Length;
			target.WriteBytes(data, offset, count);

			result = target.ReadBytes(0, data.Length);
			Assert.AreEqual(data.Length, result.Length);
			for (int i = 0; i < data.Length; i++)
			{
				Assert.AreEqual(data[i], result[i]);
			}
			result = new byte[size];
			target.ReadBytes(result,2, 1, 1);
			Assert.AreEqual(result[2], data[1]);
			Assert.AreNotEqual(result[3], data[2]);

			result = target.ReadBytes(1, 1);
			Assert.AreEqual(2, result[0]);
			Assert.AreEqual(1, result.Length);
			target.Dispose();
		}
		/// <summary>
		///A test for SynchronizedRead
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void SynchronizedReadTest()
		{
			int size = 100;
			string name = "test";
			MemoryMappedFile target = new MemoryMappedFile(size, name);

			byte[] result = new byte[size];

			int count = target.SynchronizedRead(result, 0, 0, size);
			Assert.AreEqual(size, count);

			byte[] data = new byte[] { 1, 2, 3, 4, 5 };
			int offset = 0;
			count = data.Length;
			target.WriteBytes(data, offset, count);

			count = target.SynchronizedRead(result, 0, 0, data.Length);
			Assert.AreEqual(data.Length, count);
			for (int i = 0; i < data.Length; i++)
			{
				Assert.AreEqual(data[i], result[i]);
			}
			target.Dispose();
		}

		/// <summary>
		///A test for MemoryMappedFile Constructor
		///</summary>
		[TestMethod()]
		public void MemoryMappedFileConstructorTest1()
		{
			int size = 1024;
			MemoryMappedFile target = new MemoryMappedFile(size);
			Assert.IsNotNull(target);
			Assert.AreEqual(size, target.Size);
			target.Dispose();
		}

		/// <summary>
		///A test for MemoryMappedFile Constructor
		///</summary>
		[TestMethod()]
		public void MemoryMappedFileConstructorTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile target = new MemoryMappedFile(size, name);
			Assert.IsNotNull(target);
			Assert.AreEqual(size, target.Size);
			Assert.AreEqual(name, target.Name);
			target.Dispose();
		}


		/// <summary>
		///A test for MemoryMappedFile Constructor
		///</summary>
		[TestMethod()]
		public void MemoryMappedFileConstructorTest3()
		{
			string fileName = Path.Combine(TestContext.TestDeploymentDir, "temp.dat");
			File.WriteAllBytes(fileName, new byte[] { 1, 2, 3, 4, 5 });
			MemoryMappedFile target = new MemoryMappedFile(fileName);
			Assert.AreEqual(5, target.Size);
			target.Dispose();
			File.Delete(fileName);
		}

		/// <summary>
		///A test for MemoryMappedFile Constructor
		///</summary>
		[TestMethod()]
		public void MemoryMappedFileConstructorTest2()
		{
			string fileName = Path.Combine(TestContext.TestDeploymentDir, "temp.dat");
			string name = "test";
			File.WriteAllBytes(fileName, new byte[] { 1, 2, 3, 4, 5 });
			MemoryMappedFile target = new MemoryMappedFile(fileName, name);
			Assert.AreEqual(5, target.Size);
			Assert.AreEqual(name, target.Name);
			target.Dispose();
			File.Delete(fileName);
		}
	}
}
